package com.speedata.uhf;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.speedata.libuhf.IUHFService;
import com.speedata.libuhf.bean.HoptableData;
import com.speedata.libuhf.bean.InvFrpData;

import java.util.List;

public class DebugActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvbInvPw, tvbInvFpw, tvaInvPw, tvaInvFpw, tvInvPw, tvInvC, tvInvTemp;
    private TextView tvFreqHopInfo, tvModuleInfo;
    private EditText etPower, etFreq;
    private IUHFService iuhfService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug);
        initView();
        iuhfService = MyApp.getInstance().getIuhfService();
    }

    private void initView() {
        tvbInvPw = findViewById(R.id.tv_binvpw);
        tvbInvFpw = findViewById(R.id.tv_binvfpw);
        tvaInvPw = findViewById(R.id.tv_ainvpw);
        tvaInvFpw = findViewById(R.id.tv_ainvfpw);
        tvInvPw = findViewById(R.id.tv_invpw);
        tvInvC = findViewById(R.id.tv_invc);
        tvInvTemp = findViewById(R.id.tv_invtemp);
        findViewById(R.id.btn_get_afpow).setOnClickListener(this);
        etPower = findViewById(R.id.et_power);
        etFreq = findViewById(R.id.et_freq);
        findViewById(R.id.btn_get_frehop).setOnClickListener(this);
        tvFreqHopInfo = findViewById(R.id.tv_frehop_info);
        tvModuleInfo = findViewById(R.id.tv_module_info);
        findViewById(R.id.btn_get_module_info).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_get_afpow:
                getAfPower();
                break;
            case R.id.btn_get_frehop:
                getFreqHop();
                break;
            case R.id.btn_get_module_info:
                getModuleInfo();
                break;
            default:
                break;
        }
    }

    private void getFreqHop() {
        tvFreqHopInfo.setText("频率              RL(dB)          VRWR          天线\n");
        List<HoptableData> list = iuhfService.getFreqHoptable();
        if (list != null) {
            for (HoptableData hoptableData : list) {
                tvFreqHopInfo.append(hoptableData.getFreq() + "             -"
                        + hoptableData.getRl() + "          "
                        + hoptableData.getVrwr() + "               "
                        + hoptableData.getAnt() + "\r\n");
            }
        }
    }

    @SuppressLint({"SetTextI18n", "DefaultLocale"})
    private void getAfPower() {
        InvFrpData invFrpData = new InvFrpData();
        int power = 30 * 100;
        int freq = 915250;
        try {
            power = (Integer.parseInt(etPower.getText().toString())) * 100;
            freq = Integer.parseInt(etFreq.getText().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        int res = iuhfService.getInvFRPower(power, freq, invFrpData);
        if (res == 0) {
            tvbInvPw.setText(getString(R.string.constr_carry_binvpw)
                    + String.format("%.1f", invFrpData.ibpower)
                    + "(" + String.format("%.1f", invFrpData.ibpowerAds) + ")");
            tvbInvFpw.setText(getString(R.string.constr_carry_binvfpw)
                    + String.format("%.1f", invFrpData.ibfpower)
                    + "(" + String.format("%.1f", invFrpData.ibfpowerAds) + ")");
            tvInvPw.setText(getString(R.string.constr_carry_invpw)
                    + String.format("%.1f", invFrpData.ipower)
                    + "(" + String.format("%.1f", invFrpData.ipowerAds) + ")");
            tvaInvPw.setText(getString(R.string.constr_carry_ainvpw)
                    + String.format("%.1f", invFrpData.iapower)
                    + "(" + String.format("%.1f", invFrpData.iapowerAds) + ")");
            tvaInvFpw.setText(getString(R.string.constr_carry_ainvfpw)
                    + String.format("%.1f", invFrpData.iafpower)
                    + "(" + String.format("%.1f", invFrpData.iafpowerAds) + ")");
            tvInvC.setText(getString(R.string.constr_carry_invc) + invFrpData.tagcnt);
            tvInvTemp.setText(getString(R.string.constr_carry_invtep) + invFrpData.temperature + "°C");
        }
    }

    private void getModuleInfo() {
        String info = "module:" + iuhfService.getModuleDetails();
        info += "\nhard:" + iuhfService.getHardwareVer();
        info += "\nsoft:" + iuhfService.getSoftwareVer();
        tvModuleInfo.setText(info);
    }
}
